package engine;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import enemies.Enemy;



public class Ninja {
	private int x, y;
	private double speedX, speedY, jump, damage, vit;
	private boolean jumping = false, jumpable = true, movingLeft = false, movingRight = false,
			swordAttacking = false, rightWall = false, leftWall = false;
	private Game game;
	private final int INICIALSPEED = 6;
	private final int INICIALJUMP = 20;
	private int HEIGHT = 50;
	private int WIDTH=20;

	//images
	private BufferedImage ninjaNormal, ninjaJumping, ninjaRightWall, ninjaLeftWall;
	private ArrayList<Frame> runningLeft = new ArrayList<Frame>();
	private ArrayList<Frame> runningRight = new ArrayList<Frame>();
	private ArrayList<Image> swordAttack = new ArrayList<Image>();
	private int ticks=0, frame=0, swordticks = 0;
	private boolean framesReverse=false;

	public Ninja(Game game){
		// Image Setups
		ninjaNormal = game.createImage("/data/ninja.png");
		//ninjaNormal = game.resize(ninjaNormal, WIDTH, HEIGHT);
		
		ninjaJumping = game.createImage("/data/ninjaJump.png");
		
		ninjaRightWall = game.createImage("/data/ninja_wall_right.png");
		ninjaLeftWall = game.createImage("/data/ninja_wall_left.png");
		
		runningLeft.add(new Frame(game.createImage("/data/ninja_running_left_1.png"), 40));
		runningLeft.add(new Frame(game.createImage("/data/ninja_running_left_2.png"), 40));
		runningLeft.add(new Frame(game.createImage("/data/ninja_running_left_3.png"), 40));

		runningRight.add(new Frame(game.createImage("/data/ninja_running_right_1.png"), 40));
		runningRight.add(new Frame(game.createImage("/data/ninja_running_right_2.png"), 40));
		runningRight.add(new Frame(game.createImage("/data/ninja_running_right_3.png"), 40));
		
		
		swordAttack.add(game.createImage("/data/corte1.png"));
		swordAttack.add(game.createImage("/data/corte2.png"));
		swordAttack.add(game.createImage("/data/corte3.png"));

		speedX = INICIALSPEED;
		speedY = 0;
		damage = 5;
		vit = 20;
		jump = INICIALJUMP;
		x = game.getWidth()/2;
		y = game.getHeight()-HEIGHT;
		this.game = game;
	}
	
	
	
	public void update(){
		if (movingLeft && x>0 && !(leftWall && jumping))
			x -= speedX;
		else if(movingLeft && x<=0 && !(leftWall && jumping)){
			speedY -= speedY/2;
			jumpable = true;
			leftWall = true;
		}
		else if (movingRight && x<game.getWidth()-WIDTH && !(rightWall && jumping))
			x += speedX;
		else if(movingRight && x>=game.getWidth()-WIDTH && !(rightWall && jumping)){
			speedY -= speedY/2;
			jumpable = true;
			rightWall = true;
		}
		if (jumping){
			jumpable = false;
			y -= jump;
			jump--;
			if (rightWall)
				x-= speedX;
			if (leftWall)
				x+=speedX;
			if ((rightWall && x<game.getWidth()-(WIDTH*6)) || (leftWall && x>WIDTH*5) ){
				rightWall = false;
				leftWall = false;
			}
				
			if (jump<=0){
				jumping=false;
				jump = INICIALJUMP;
			}
		}

		if (y<game.getHeight()-HEIGHT && !jumping){
			speedY--;
			y -= speedY;
		}

		if (y >= game.getHeight()-HEIGHT){
			jumpable = true;
			speedY=0;
			y = game.getHeight()-HEIGHT;
		}

		for (Platform p: game.getPlatforms()){
			if (x>=p.getX()-WIDTH && x<=p.getxLength()+p.getX() 
					&& y+HEIGHT <= p.getY() + p.getyLength() && y+HEIGHT >= p.getY()+speedY-game.getGameSpeed()*2){
				jumpable = true;
				speedY=game.getGameSpeed();
				y = p.getY() - (int)(HEIGHT -game.getGameSpeed());
				leftWall = false;
				rightWall = false;
			}
		}

		if (y<-50)
			jumpable = false;
	}

	public boolean isJumping() {
		return jumping;
	}

	public void jump() {
		if (jumpable)
			jumping = true;
	}

	public void moveLeft(){
		movingLeft = true;
	}

	public void moveRight(){
		movingRight = true;
	}

	public double getSpeed() {
		return speedX;
	}

	public void setSpeed(double speed) {
		this.speedX = speed;
	}

	public double getJump() {
		return jump;
	}

	public void jump(double jump) {
		this.jump = jump;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public void stopLeft() {
		movingLeft = false;
	}

	public void stopRight() {
		movingRight = false;
	}


	public Image getImage(){
		Image image = Toolkit.getDefaultToolkit().getImage("data/ninja_running_left_1.png");
		int width = image.getWidth(null);
		int height = image.getHeight(null);
		if (rightWall && x>=game.getWidth()-WIDTH-1)
			return ninjaRightWall;
		else if (leftWall && x== 0)
			return ninjaLeftWall;
		else if (jumping)
			return ninjaJumping;
		else if(movingRight){
			if(ticks<=0){
				if(framesReverse){
					frame--;
				}else{
					frame++;
				}
				if(frame==0){ // se atingir os zero ticks passa de frame
					framesReverse=false;
				}else if(frame>runningLeft.size()-1){
					framesReverse=true;
					frame=frame-2;
				}
				ticks=runningRight.get(frame).getTime();
			}
			ticks--;
			return runningRight.get(frame).getImage();
		}else if(movingLeft){
			if(ticks<=0){
				if(framesReverse){
					frame--;
				}else{
					frame++;
				}
				if(frame==0){ // se atingir os zero ticks passa de frame
					framesReverse=false;
				}else if(frame>runningLeft.size()-1){
					framesReverse=true;
					frame=frame-2;
				}
				ticks=runningLeft.get(frame).getTime();
			}
			ticks--;
			return runningLeft.get(frame).getImage();
		}
		else
			return ninjaNormal;
	}

	public Image getSwordEffects(){
		swordticks++;
		if (swordticks < 20)
			return swordAttack.get(1);
		else if (swordticks < 40)
			return swordAttack.get(0);
		else if (swordticks < 60)
			return swordAttack.get(2);
		else{
			swordAttacking = false;
			return null;
		}
			
			
	}

	public void swordAttack(){
		swordAttacking = true;
		swordticks = 0;
		ArrayList<Enemy> enemies = game.getEnemies();
		for (Enemy e: enemies){
			if (e.getX() > (x- 2*getWIDTH()) && e.getX() < (x + 2*getWIDTH()) 
					&& e.getY() > (y -getHEIGHT()) && e.getY() < (y + (3*getHEIGHT()/2))){
				e.getHit(damage);
			}
		}
	}


	public int getHEIGHT() {
		return HEIGHT;
	}

	public int getWIDTH() {
		return WIDTH;
	}

	private int getImageWidth(){
		return WIDTH; // TODO
	}

	private int getImageHeight(){
		return HEIGHT; // TODO
	}

	public double getVit() {
		return vit;
	}

	public boolean isSwordAttacking() {
		return swordAttacking;
	}




}
